package com.enlorenz.core.channel.socket.common.reader.custom;

import java.io.InputStream;

import com.enlorenz.common.util.MessageBuffer;
import com.enlorenz.core.channel.socket.common.reader.ISocketReader;

public class AsciiMsgSocketReader implements ISocketReader{
	private InputStream inputStream;
	
	@Override
	public void setInputStream(InputStream inputStream) {
		this.inputStream=inputStream;
	}
	
	@Override
	public byte[] read()
			throws Exception {
		MessageBuffer messageBuffer=new MessageBuffer();
		byte [] msg=new byte[1];
		boolean isLegal=false;
		boolean isReadEnd=false;
		while(-1!=inputStream.read(msg)){
			byte b=msg[0];
			if(b == '*'){
				messageBuffer.append(msg);
				msg=new byte[1];
				isLegal=true;
				continue;
			}else if(b == '#'){
				messageBuffer.append(msg);
				msg=new byte[1];
				isReadEnd=true;
				break;
			}
			
			if(isLegal){
				messageBuffer.append(msg);
			}
			msg=new byte[1];
		}
		
		byte [] output=messageBuffer.toByte();
		
		if(isLegal && isReadEnd && null!=output){
			return output;
		}else{
			return null;
		}
		
	}

	
}
